/**
 * 
 */
package data.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Dient als Projekt- und Entwickler-Register. Abgeleitet von
 * {@link LinkedHashMap}&lt;projectId, project&gt;
 * 

 */
public class ProjectsCollection extends LinkedHashMap<String, Project> {
	private static final long serialVersionUID = 8542682658392461165L;
	private final Map<String, Developer> developers = new LinkedHashMap<String, Developer>();

	/**
	 * 
	 */
	public ProjectsCollection() {
		super();
	}

	/**
	 * @param initialCapacity
	 * @param loadFactor
	 * @param accessOrder
	 */
	public ProjectsCollection(int initialCapacity, float loadFactor,
			boolean accessOrder) {
		super(initialCapacity, loadFactor, accessOrder);
	}

	/**
	 * @param initialCapacity
	 * @param loadFactor
	 */
	public ProjectsCollection(int initialCapacity, float loadFactor) {
		super(initialCapacity, loadFactor);
	}

	/**
	 * @param initialCapacity
	 */
	public ProjectsCollection(int initialCapacity) {
		super(initialCapacity);
	}

	/**
	 * @param m
	 */
	public ProjectsCollection(Map<? extends String, ? extends Project> m) {
		super(m);
	}

	public List<Project> getAllProjects() {
		return new ArrayList<Project>(super.values());
	}

	public List<Process> getAllProcesses() {
		List<Process> processes = new ArrayList<Process>();
		for (Project project : values()) {
			for (ProjectWeek projectWeek : project.getProjectWeeks()) {
				processes.addAll(projectWeek.getProcesses());
			}
		}
		return processes;
	}

	public List<Developer> getAllDevelopers() {
		return new ArrayList<Developer>(developers.values());
	}

	public List<ProjectDeveloper> getAllProjectDevelopers() {
		List<ProjectDeveloper> projectDevelopers = new ArrayList<ProjectDeveloper>();
		for (Project project : super.values()) {
			projectDevelopers.addAll(project.getInvolvedDevelopers());
		}
		return projectDevelopers;
	}

	private Developer getDeveloper(String developerId) {
		Developer developer = developers.get(developerId);
		if (developer == null) {
			developer = new Developer(developerId);
			developers.put(developerId, developer);
		}
		return developer;
	}

	public Process addProcess(ProjectWeek projectWeek, String developerId,
			Short effortRequirementsEngineering,
			Short effortArchitectureAndDesign,
			Short effortImplementationAndIntegration, Short effortBuildAndTest,
			Short effortProjectManagement) {
		if (effortRequirementsEngineering == null
				|| effortArchitectureAndDesign == null
				|| effortImplementationAndIntegration == null
				|| effortBuildAndTest == null
				|| effortProjectManagement == null) {
			return new Process(projectWeek, getDeveloper(developerId));
		}
		return new Process(projectWeek, getDeveloper(developerId),
				effortRequirementsEngineering, effortArchitectureAndDesign,
				effortImplementationAndIntegration, effortBuildAndTest,
				effortProjectManagement);
	}

}
